package com.xwx.common.utils;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;
import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 
* @Description:自动生成model与controller 
* @author:Dream QQ:68697015
* @time:2015年8月28日 上午11:34:44
 */
public class MySqlAutoBuiderClassUtils {
	
	protected static Logger log=Logger.getLogger(MySqlAutoBuiderClassUtils.class);
	private static Configuration config=null;
	private static Template temp = null;
	static{
		config = new Configuration();
		config.setClassForTemplateLoading(MySqlAutoBuiderClassUtils.class, "/");	
	}
	/**
	 * 生成model
	 * @param list
	 * @throws Exception
	 */
	private static void CreateModel()throws Exception{
		temp=config.getTemplate(PropKit.get("modelTemplate"));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("modelPackage", PropKit.get("modelPackage"));//包名
		List<Record> list=getTables();
		for (Record r : list) {
			String className=StrKit.firstCharToUpperCase(r.getStr("TABLE_NAME"))+"Model";//类名
			map.put("className",className);
			map.put("date", DateUtils.getCurrentTime());//生成时间
			map.put("descption", r.getStr("TABLE_COMMENT"));//类描述
			//设置字段
			List<Record> columns=getColumnsByTableName(r.getStr("TABLE_NAME"));
			map.put("columns",columns);
			File cf = new File(System.getProperty("user.dir")+"/src/"+PropKit.get("modelPackage").replace(".", "/"));
			cf.mkdirs();
			temp.process(map, new FileWriter(cf+"/"+className+".java"));			
		}
		log.info("model类生成成功！");
	}
	/**
	 * 生成controller
	 * @throws Exception
	 */
	private static void CreateController()throws Exception{
		temp=config.getTemplate(PropKit.get("controllerTemplate"));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("controllerPackage", PropKit.get("controllerPackage"));
		map.put("modelPackage", PropKit.get("modelPackage"));
		List<Record> list = getTables();
		for (Record r : list) {
			String className=StrKit.firstCharToUpperCase(r.getStr("TABLE_NAME"))+"Controller";//类名
			map.put("className",className);
			map.put("date", DateUtils.getCurrentTime());//生成时间
			map.put("descption", r.getStr("TABLE_COMMENT"));//类描述
			map.put("modelClass", StrKit.firstCharToUpperCase(r.getStr("TABLE_NAME"))+"Model");
			File cf = new File(System.getProperty("user.dir")+"/src/"+PropKit.get("controllerPackage").replace(".", "/"));
			cf.mkdirs();
			temp.process(map, new FileWriter(cf+"/"+className+".java"));			
		}
		log.info("Controller类生成成功!");
	}
	/**
	 * 获取全部表结构
	 * @return list
	 */
	public static List<Record> getTables(){
		return Db.find("select TABLE_NAME,TABLE_COMMENT from information_schema.`TABLES` where TABLE_SCHEMA='"+PropKit.get("dbName")+"'");
	}
	/**
	 * 根据表名称获取字段结构
	 * @param tableName
	 * @return
	 */
	public static List<Record> getColumnsByTableName(String tableName){
		return Db.find("select COLUMN_NAME,COLUMN_DEFAULT,IS_NULLABLE,DATA_TYPE,COLUMN_COMMENT from information_schema.`COLUMNS` where TABLE_NAME='"+tableName+"'");
	}
	/**
	 * 自动生成model与controller
	 * @param model true生成model
	 * @param controller true生成controller
	 * @throws Exception
	 */
	public static void runAutoBuider(boolean model,boolean controller)throws Exception{
		PropKit.use("appConfig.txt");
		DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("web_url"), PropKit.get("web_user"), PropKit.get("web_password").trim());
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		druidPlugin.start();
		arp.start();
		if(model)CreateModel();
		if(controller)CreateController();
	}
}
